<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoinClue;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ClueController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title('线索')
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid(request('coin_id', 0)));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($coin_id)
    {
        return Grid::make(new CoinClue(), function (Grid $grid) use ($coin_id) {
            $grid->model()->where('coin_id', $coin_id);

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('content', '内容')->if(function () {
                return $this->type == \App\Models\CoinClue::TYPE_IMAGE;
            })->image(50, 50)->else()->display(function () {
                return $this->content;
            });
            $grid->column('is_circle', '圆圈形式')->display(function ($is_circle) {
                return \App\Models\CoinClue::IS_CIRCLE[$is_circle];
            });
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('updated_at', 'desc');
            $grid->model()->setConstraints([
                'coin_id' => $coin_id,
            ]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoinClue(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type')->using(\App\Models\CoinClue::TYPE);
            $show->field('content', '内容')->as(function ($content) {
                return $this->type == \App\Models\CoinClue::TYPE_IMAGE ? '<img src="' . $content . '" style="width: 100px;height: 100px;">' : $content;
            })->unescape();
            $show->field('amount', '橡果数');
            $show->field('number', '拉新数');
            $show->field('is_circle', '圆圈形式')->using(\App\Models\CoinClue::IS_CIRCLE);

            $show->disableDeleteButton();
            $show->disableListButton();
            $show->disableEditButton();
            $show->tools('<div class="btn-group pull-right" style="margin-right: 5px"><a href="/admin/clues?coin_id=' . request('coin_id', 0) . '" class="btn btn-sm btn-primary "><i class="feather icon-list"></i><span class="d-none d-sm-inline"> 列表</span></a></div>');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoinClue(), function (Form $form) {
            $coin_id = request('coin_id', 0);
            $form->text('name');
            $form->radio('type')
                ->options(\App\Models\CoinClue::TYPE)
                ->default(\App\Models\CoinClue::TYPE_TEXT)
                ->when(\App\Models\CoinClue::TYPE_TEXT, function (Form $form) {
                    $form->text('content', '内容');
                })->when(\App\Models\CoinClue::TYPE_IMAGE, function (Form $form) {
                    $form->image('image', '内容')->retainable()->removable(false)->autoUpload();
                    if ($form->model()->content) {
                        $form->html('<img src="' . $form->model()->content . '" height="200px" width="200px"/>');
                    }
                });
            $form->number('amount', '橡果数');
            $form->number('number', '拉新数');
            $form->radio('is_circle', '圆圈形式')
                ->options(\App\Models\CoinClue::IS_CIRCLE)
                ->default(\App\Models\CoinClue::IS_CIRCLE_YES);
            $form->hidden('coin_id')->value($coin_id);

            $form->saving(function (Form $form) {
                if ($form->type == \App\Models\CoinClue::TYPE_IMAGE) {
                    $form->content = $form->image;
                }
                $form->deleteInput('image');
            });

            $form->disableListButton();
            $form->disableViewButton();
            $form->disableViewCheck();
            $form->disableDeleteButton();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
            $form->tools('<div class="btn-group pull-right" style="margin-right: 5px"><a href="/admin/clues?coin_id=' . request('coin_id', 0) . '" class="btn btn-sm btn-primary "><i class="feather icon-list"></i><span class="d-none d-sm-inline"> 列表</span></a></div>');
        });
    }
}
